//
//  HomeView.m
//  Pictorial
//
//  Created by SKY on 15/12/28.
//  Copyright © 2015年 sky. All rights reserved.
//

#import "HomeView.h"

#define LABEL_HEIGHT    20
#define LABEL_WIDTH     200

@interface HomeView ()

@property (nonatomic,weak) UILabel *timeLabel;

@end

@implementation HomeView

-(void)willMoveToSuperview:(UIView *)newSuperview{
    
    self.frame = newSuperview.bounds;
    self.backgroundColor = [UIColor whiteColor];
    self.showsHorizontalScrollIndicator = NO;
    self.showsVerticalScrollIndicator = NO;
    self.pagingEnabled = YES;
    
    UILabel *alertLabel = [UILabel labelWithText:@"明天的画报" fontSize:15 frame:CGRectMake(-LABEL_WIDTH, self.frame.size.height * 0.5 - LABEL_HEIGHT * 2, LABEL_WIDTH, LABEL_HEIGHT) color:[UIColor blackColor] textAlignment:2];
    [self addSubview:alertLabel];
    
    UILabel *timeLabel = [UILabel labelWithText:@"" fontSize:15 frame:CGRectMake(-LABEL_WIDTH, self.frame.size.height * 0.5 - LABEL_HEIGHT, LABEL_WIDTH, LABEL_HEIGHT) color:[UIColor grayColor] textAlignment:2];
    [self addSubview:timeLabel];
    
    self.timeLabel = timeLabel;

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
    [timer fire];
    
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    
}

- (void)timerFireMethod:(NSTimer*)theTimer
{
    
    NSCalendar *cal = [NSCalendar currentCalendar];//定义一个NSCalendar对象

    NSDateComponents *adcomps = [[NSDateComponents alloc] init];
    [adcomps setYear:[[[NSDate today] year] integerValue]];
    [adcomps setMonth:[[[NSDate today] month_true] integerValue]];
    [adcomps setDay:[[[NSDate today] day] integerValue] + 1];

    NSDate *newdate = [cal dateFromComponents:adcomps];

    NSDate *today = [NSDate date];//得到当前时间
    //用来得到具体的时差
    unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *dateComponents = [cal components:unitFlags fromDate:today toDate:newdate options:0];

    self.timeLabel.text = [NSString stringWithFormat:@"%ld时%ld分%ld秒", [dateComponents hour], [dateComponents minute], [dateComponents second]];

}
@end
